<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>

</body>
<script>
	// 使用循环实现管道函数
	{
		function pipe(...args){
			return function (value){
				let result = value
				for(let fn of args){
					result = fn(result)
				}
				return result
			}
		}
		let a = x => x + 1
		let b = x => x * 10
		let c = x => x / 2
		let process = pipe(a,b,c)
		console.log(process(1))
	}
	
	// 使用递归实现管道函数
	{
		function pipe(...args){
			return (value) => {
				if(args.length === 0) return value
				const [firstFn,...otherFn] = args
				return pipe(...otherFn)(firstFn(value))
			}
		}
		let a = x => x + 1
		let b = x => x * 10
		let c = x => x / 2
		let process = pipe(a,b,c)
		console.log(process(1))
	}
	
	// 使用reduce实现管道函数
	{
		function pipe(...args){
			return (value) => args.reduce((acc,fn)=>fn(acc),value)
		}
		let a = x => x + 1
		let b = x => x * 10
		let c = x => x / 2
		let process = pipe(a,b,c)
		console.log(process(1))
	}
</script>
</html>